package utility;

import java.util.*;
import java.text.*;

public class DateConversion 
{
	String date;
	String pattern  = "ddMMyyyy";
	char c;
	String Outputpattern = "yyyy-dd-MM";
	String dateTimePattern = "yyyy-dd-MM HH:MM:SS";
	//TimeZone zone ;
	//Calendar calender;
		
	public String getDate(String date,String Outputpattern)
	{
	
		try
		{
			this.Outputpattern = Outputpattern;
			System.out.println("utility::DateConversion::getDate()::the pattern is "+Outputpattern);
			this.date = date;					
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return setDate();
		
	}
	
	public String getDate(Date date,String Outputpattern)
	{
		try
		{
			this.Outputpattern = Outputpattern;
			System.out.println("utility::DateConversion::getDate():: the pattern is "+Outputpattern);
			//SimpleDateFormat sdm = new SimpleDateFormat(this.pattern);
			//this.date = sdm.format(date);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return this.date;
	}
	
	
	public String getDate(String date)
	{
		try
		{
			SimpleDateFormat sdm = new SimpleDateFormat(this.Outputpattern);
			Date d = sdm.parse(date);
			this.date = sdm.format(d);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return this.date;
	}
	
	public String getDate()
	{
		SimpleDateFormat sdm = new SimpleDateFormat(this.Outputpattern);
		this.date = sdm.format(sdm);
		return date;
	}
		
		
	public String setDate()
	{
		int day = getData('d',date);
		System.out.println("utility::DateConversion::setDate():: Day :"+day);
		int month = getData('M',date);
		System.out.println("utility::DateConversion::setDate():: Month:"+month);
		int year = getData('y',date);
		System.out.println("utility::DateConversion::setDate():: Year :"+year);
		
		StringBuffer temp = new StringBuffer(Outputpattern);
		StringBuffer newdate = new StringBuffer();
		
		for(int i=0;i<temp.length();i++)
		{
			if(temp.charAt(i)== 'd')
			{
				System.out.println("utility::DateConversion::setDate():: d found");
				newdate.append(day+"-");
				i = i+2;
				continue;
			}
			else if(temp.charAt(i)=='M')
			{
				System.out.println("utility::DateConversion::setDate():: M found");
				newdate.append(month+"-");
				i = i+2;
				continue;
			}
			else if(temp.charAt(i)=='y')
			{
				System.out.println("utility::DateConversion::setDate():: y found");
				newdate.append(year+"-");
				i = i+4;
				continue;
			}
		}
		this.date = newdate.subSequence(0, newdate.length()-1).toString();
		return date;
	}
	
	
	// Returns current day in integer format
	public int getDay(String date)
	{
		this.date = date;
		int day = getData('d');	
		return day;
	}
	
	
	
	// Returns current month in integer format
	public int getMonth(String date)
	{
		this.date = date;
		int month = getData('M');	
		return month;
	}
	
	// Returns current year in integer format
	public int getYear(String date)
	{
		this.date = date;
		int year = getData('y');
		return year;
	}
	
	// Returns current hour in 24-Hour format
	public int getHour()
	{
		int hour = getData('H');	
		return hour;
	}
	
	
	
	// Returns current minutes
	public int getMinutes()
	{
		int minutes = getData('m');	
		return minutes;
	}
	
	
	
	//returns current seconds 
	public int getSeconds()
	{
		int seconds = getData('s');	
		return seconds;
	}
	
	
	
	// This method accepts characters for date format and then returns data related to that specific character
	// 'y' for Year
	// 'M' for Month
	// 'd' for Day
	// 'H' for Hour
	// 'm' for Minutes
	// 's' for Seconds
	// It operates on current date
	
	public int getData(char c)
	{
		int data = 0;
		this.c = c;
		StringBuffer sbrDate = new StringBuffer();
		System.out.println("utility::DateConversion::getData():: The pattern is "+pattern);
		sbrDate.append(pattern);
		
		int beginIndex = sbrDate.toString().indexOf(c);
		//System.out.println("begin index "+beginIndex);
		int endIndex = sbrDate.toString().lastIndexOf(c)+1;
		//System.out.println("end index "+endIndex);
				
		sbrDate = new StringBuffer();
		sbrDate.append(date);
		data = Integer.parseInt(sbrDate.subSequence(beginIndex, endIndex).toString());
		System.out.println("utility::DateConversion::getData():: Data"+data);
		return data;
	}
		
	
	//This method accepts character for date specific data  and date in string format
	// and returns data specific to given character
	public int getData(char c, String date)
	{
		int data = 0;
		this.c = c;
		
		System.out.println("utility::DateConversion::getData()::The date in getData() is "+date);
		StringBuffer sbrDate = new StringBuffer();
		sbrDate.append(pattern);
		
		int beginIndex = sbrDate.toString().indexOf(c);
		System.out.println("utility::DateConversion::getData()::begin index "+beginIndex);
		int endIndex = sbrDate.toString().lastIndexOf(c)+1;
		System.out.println("utility::DateConversion::getData()::end index "+endIndex);
				
		sbrDate = new StringBuffer();
		sbrDate.append(date);
		data = Integer.parseInt(sbrDate.subSequence(beginIndex, endIndex).toString());
		return data;
	}
	
	
	
	
	public int getDaysInMonth(int Month, int year)
	{
		int daysInMonth = 0;
		if(Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12)
		{
			daysInMonth = 31;
		}
		else if(Month == 2)
		{
			if(year%4 == 0)
			{
				daysInMonth = 29;
			}
			else
			{
				daysInMonth = 28;
			}
		}
		else
		{
			daysInMonth = 30;
		}
		return daysInMonth;
	}
	
	
	public int getDaysInYear(int year)
	{
		int daysInYear = 0;
		
		if(year%4 == 0)
		{
			daysInYear = 366;
		}
		else
		{
			daysInYear = 365;
		}
		return daysInYear;
	}
	
	
	public int getDaysCount(int month, int year, String period)
	{
		int days = 0;
		if(period.equals("quarterly"))// four times in year
		{
			days = getDaysInMonth(month, year)+getDaysInMonth(month+1, year)+ getDaysInMonth(month+2, year)+ getDaysInMonth(month+3, year);
		}
		else if(period.equals("biannually"))// twice in year
		{
			days = getDaysInMonth(month, year)+getDaysInMonth(month+1, year)+ getDaysInMonth(month+2, year)+ getDaysInMonth(month+3, year)+getDaysInMonth(month+4, year)+getDaysInMonth(month+5, year);
		}
		else if(period.equals("annually") || period.equals("yearly"))// once in year
		{
			days = getDaysInYear(year);
		}
		else if(period.equals("biennial"))// once in two years
		{
			days = getDaysInYear(year)+getDaysInYear(year+1);
		}
		return days;
	}
	
	
	//	 Get next date by adding given number of days
	public String getNextDate(String date , int days)
	{
		DateConversion d = new DateConversion();
		int newDays = days + d.getDay(date);
		String newdate = d.getDate(d.getMonth(date)+"-"+newDays+"-"+d.getYear(date));
		return newdate;
	}

	
	//	Check if given date is todays date
	// For minutes and seconds it checks whether given min and seconds are <= system date
	public boolean isTodaysDate(String date)
	{
		System.out.println("utility::DateConversion::isTodaysDate()::The date given by user is "+date);
		DateConversion d = new DateConversion();
		boolean TodaysDate = false;
		int userday = getData('d',date);
		int usermonth = getData('M',date);
		int useryear = getData('y',date);
		//int userhour = getData('H',date);
		//int userminutes = getData('m',date);
		//int userseconds = getData('s',date);
		
		if(userday == d.getDay(date) && usermonth == d.getMonth(date) && useryear == d.getYear(date))/* && userhour == d.getHour() && userminutes <= d.getMinutes() && userseconds <= d.getSeconds()*/
		{
			TodaysDate = true;
		}
		return TodaysDate;
	}

	public static void main(String arg[])
	{
		//MyDate mdf = new MyDate();
		//MyDate mdf = new MyDate("yyyy-dd-MM");
		//MyDate mdf = new MyDate("01-14-2007 16:56:00");
		//System.out.println("date "+mdf.getDate());
		//System.out.println("month 	"+mdf.getMonth());
		//System.out.println("day 	"+mdf.getDay());
		//System.out.println("year 	"+mdf.getYear());
		//System.out.println("hour "+mdf.getHour());
		//System.out.println("min "+mdf.getMinutes());
		//System.out.println("sec "+mdf.getSeconds());
		//String date1 = "01-14-2007 13:13:50";
		//System.out.println(mdf.isTodaysDate(date1));
		//MyDate d =  mdf.getNextDate(mdf.getDate(), 40);
		DateConversion d = new DateConversion();
		System.out.println("date "+d.getDate("19012007","yyyy-MM-dd"));
		System.out.println("month 	"+d.getMonth("19012007"));
		System.out.println("year 	"+d.getYear("19012007"));
		System.out.println("day 	"+d.getDay("19012007"));
	}
}
